/**
 * 
 */
var xmlHttp=null ;
var g_basisObj = null; 	//包括用户信息及navbar信息等
                        //当是游客信息时，其user变量为空字符串
var g_appObj = null;	//每个具体模块的全局对象
var g_pageObj = null;

/*形如：
 * "{"
+"   'user':'dqjbd',"
+"   'id':'role0',"
+"   'topic':["
+"       {"
+"          'name':'基本信息',"  	//topic[0].name
+"          'item':["			//topic[0].item[0].name
+"              {'name':'完善个人信息','value':'peinfo'},"
+"              {'name':'密码修改','value':'repwd'}      "
+"           ]"
+"        },"
+"       {"
+"          'name':'基本功能',"
+"          'item':["
+"              {'name':'报告上传','value':'reportsubmit'},"
+"              {'name':'进行考试','value':'examsubmit'}      "
+"           ]"
+"        },"
+"    ]"
+"}"

 * 
 */
function init(){
	//初始化ajax通信变量
	xmlHttp = g_createHttpRQ();
	//进行菜单消息注册
	g_addNameEvent("train_info","click",startFind);
}

function startFind(e){
	var obj = e.target || window.event.srcElement;
	switch(obj.getAttribute("key")){
	case "train_basis":
		g_appObj = new TrainBasisInput();
		g_appObj.loadEngine();
		break;	
	case "train_basis_show":
		g_appObj = new TrainBasisShow();
		break;
	case "train_get":
		g_appObj = new TrainGetInput();
		g_appObj.loadEngine();
		break;	
	case "train_get_show":
		g_appObj = new TrainGetShow();
		break;
	}
		
}

function TrainBasisShow(){
	g_appObj=new TBShowFunc();
	g_pageObj=g_appObj;
	g_pageObj.loadEngine();
}

function TBShowFunc(){
	this.pagesize=10;
	this.curpage=1;
	this.size=10;
}

/*
 * trainno 1	
	start     2	
	end      3	
	starttime	4
	endtime	5
	costtime	6	
	midstation 7	
	midtime	8
*/

TBShowFunc.prototype.loadEngine = function(){
	var data ="key1=trainbasisnum&key2=trainbasissel"+"&pagesize="+this.pagesize+"&curpage="+this.curpage+"&size="+this.size;
	xmlHttp.open("post","pageshow.do", true);
	xmlHttp.onreadystatechange = g_pageObj.loadEngine_state;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);	
}

TBShowFunc.prototype.loadEngine_state = function(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			g_displayPage(xmlHttp.responseText,["车次","出发站","终点站","出发时间","到达时间","花费时间(分钟)","中转站点","经停时刻"],null,"content");
			g_addNameEvent("curpage","click",g_pageObj.searchpage);
		}
	}
}

TBShowFunc.prototype.searchpage=function(e){
	var obj = e.target || window.event.srcElement;
	g_pageObj.curpage=parseInt(obj.innerText);
	var data ="key1=trainbasisnum&key2=trainbasissel"+"&pagesize="+g_pageObj.pagesize+"&curpage="+g_pageObj.curpage+"&size="+g_pageObj.size;
	xmlHttp.open("post","pageshow.do", true);
	xmlHttp.onreadystatechange = g_pageObj.loadEngine_state;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);	
}

TBShowFunc.prototype.search=function(e){
	var data ="key1=trainbasisnum&key2=trainbasissel"+"&pagesize="+g_pageObj.pagesize+"&curpage="+g_pageObj.curpage+"&size="+g_pageObj.size;
	xmlHttp.open("post","pageshow.do", true);
	xmlHttp.onreadystatechange = g_pageObj.loadEngine_state;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);	
}

function TrainGetShow(){
	g_appObj=new TGShowFunc();
	g_pageObj=g_appObj;
	g_pageObj.loadEngine();
}

function TGShowFunc(){
	this.pagesize=10;
	this.curpage=1;
	this.size=10;
}

/*
 * trainno 1	
	start     2	
	end      3	
	starttime	4
	endtime	5
	costtime	6	
	midstation 7	
	midtime	8
*/

TGShowFunc.prototype.loadEngine = function(){
	var data ="key1=traingetnum&key2=traingetsel"+"&pagesize="+this.pagesize+"&curpage="+this.curpage+"&size="+this.size;
	xmlHttp.open("post","pageshow.do", true);
	xmlHttp.onreadystatechange = g_pageObj.loadEngine_state;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);	
}

TGShowFunc.prototype.loadEngine_state = function(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			g_displayPage(xmlHttp.responseText,["站点","上行车次","下行车次"],null,"content");
			g_addNameEvent("curpage","click",g_pageObj.searchpage);
		}
	}
}

TGShowFunc.prototype.search=function(e){
	var data ="key1=traingetnum&key2=traingetsel"+"&pagesize="+g_pageObj.pagesize+"&curpage="+g_pageObj.curpage+"&size="+g_pageObj.size;
	xmlHttp.open("post","pageshow.do", true);
	xmlHttp.onreadystatechange = g_pageObj.loadEngine_state;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);	
}

TGShowFunc.prototype.searchpage=function(e){
	var obj = e.target || window.event.srcElement;
	g_pageObj.curpage=parseInt(obj.innerText);
	var data ="key1=traingetnum&key2=traingetsel"+"&pagesize="+g_pageObj.pagesize+"&curpage="+g_pageObj.curpage+"&size="+g_pageObj.size;
	xmlHttp.open("post","pageshow.do", true);
	xmlHttp.onreadystatechange = g_pageObj.loadEngine_state;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);	
}




function TrainBasisInput(){
	
}

/*
 * trainno 1	
	start     2	
	end      3	
	starttime	4
	endtime	5
	costtime	6	
	midstation 7	
	midtime	8
*/

TrainBasisInput.prototype.loadEngine = function(){
	var s = 
		"<div style='width:1046px;margin:0 auto;background:#eeeeee;height:500px;margin-top:20px;margin-bottom:20px;padding-left:20px;padding-top:20px;border:1px solid blue;line-height:50px;'>"	
	    +"<table border='1'>"
        +"火车车次:"
        +"<input id='trainno' class='form-control' type='text' style='width:300px' /></br>"
        +"出发地："
        +"&nbsp<input id='start' class='form-control' type='text' style='width:300px' /></br>"
        +"目的地："
        +"&nbsp<input id='end' class='form-control' type='text' style='width:300px' /></br>"
        +"出发时间:"
        +"<input id='starttime' class='form-control' type='text' style='width:300px' /></br>"
        +"到达时间:"
        +"<input id='endtime' class='form-control' type='text' style='width:300px' /></br>"
        +"花费时间:"
        +"<input id='costtime' class='form-control' type='text' style='width:300px' /></br>"
        +"中间站："
        +"&nbsp<input id='midstation' class='form-control' type='text' style='width:300px' /></br>"
        +"经停时刻:"
        +"<input id='midtime' class='form-control' type='text' style='width:300px' /></br>"
		+"</table>"
		 +"<button class='btn' id='myfind'>保存</button>"
        +"</div>";
		
	
	//用于显示查询结果
	s += "<div id='showtable'></div>";
		
	var obj = document.getElementById("content");
	obj.innerHTML = s;
	
	//注册“查询”按钮消息
	g_addIdEvent("myfind", "click", g_appObj.findProc);
}

TrainBasisInput.prototype.findProc = function(){
	var trainno = g_trim_id("trainno");
	var start = g_trim_id("start");
	var end = g_trim_id("end");
	var starttime = g_trim_id("starttime");
	var endtime = g_trim_id("endtime");
	var costtime = g_trim_id("costtime");
	var midstation = g_trim_id("midstation");
	var midtime = g_trim_id("midtime");
	var data ="key=trainbasis&para="+trainno+"&para="+start+"&para="+end+"&para="+starttime+"&para="+endtime+"&para="+costtime+"&para="+midstation+"&para="+midtime;
	xmlHttp.open("post","singlesqlproc.do", true);
	xmlHttp.onreadystatechange = g_appObj.findProc_Response;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);												
}

TrainBasisInput.prototype.findProc_Response = function(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			var dataobj = g_parseJSONString(xmlHttp.responseText);
			if(dataobj.mark=="y"){
				alert("添加成功");
			}
			else alert("添加失败");
		}
	}
}

function TrainGetInput(){
	
}

/*
 * trainno 1	
	start     2	
	end      3	
	starttime	4
	endtime	5
	costtime	6	
	midstation 7	
	midtime	8
*/

TrainGetInput.prototype.loadEngine = function(){
	var s = 
		"<div style='width:1046px;margin:0 auto;background:#eeeeee;height:500px;margin-top:20px;margin-bottom:20px;padding-left:20px;padding-top:20px;border:1px solid blue;line-height:50px;'>"	
	    +"<table border='1'>"
        +"站点:"
        +"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input id='startaddress' class='form-control' type='text' style='width:300px' /></br>"
        +"上行车次:"
        +"<input id='up' class='form-control' type='text' style='width:300px' /></br>"
        +"下行车次:"
        +"<input id='down' class='form-control' type='text' style='width:300px' /></br>"
		+"</table>"
		 +"<button class='btn' id='myfind'>保存</button>"
        +"</div>";
		
		
	
	//用于显示查询结果
	s += "<div id='showtable'></div>";
		
	var obj = document.getElementById("content");
	obj.innerHTML = s;
	
	//注册“查询”按钮消息
	g_addIdEvent("myfind", "click", g_appObj.findProc);
}

TrainGetInput.prototype.findProc = function(){
	var startaddress = g_trim_id("startaddress");
	var up = g_trim_id("up");
	var down = g_trim_id("down");
	
	var data = "key=trainget&para=" +startaddress+"&para="+up+"&para="+down;
	xmlHttp.open("post", "singlesqlproc.do", true);
	xmlHttp.onreadystatechange = g_appObj.findProc_Response;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);												
}

TrainGetInput.prototype.findProc_Response = function(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			var dataobj = g_parseJSONString(xmlHttp.responseText);
				if(dataobj.mark=="y") alert("添加成功");
				else alert("添加失败");
		}
	}
}

